/* Created By TheBrodieMan */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using CommonBehaviors.Actions;
using System.Windows.Forms;
using System.Windows.Media;
using Styx;
using Styx.Common;
using Styx.CommonBot;
using Styx.CommonBot.Profiles;
using Styx.Pathing;
using Styx.TreeSharp;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;

using Honorbuddy.QuestBehaviorCore;

using Action = Styx.TreeSharp.Action;

namespace Honorbuddy.Quest_Behaviors.SpecificQuests.TroubleUnderFoot
{
	[CustomBehaviorFileName(@"14226-TroubleUnderFoot")]
	public class TroubleUnderFoot : CustomForcedBehavior
	{
		public TroubleUnderFoot(Dictionary<string, string> args)
			: base(args)
		{
			try
			{
				QuestId = 14226; //GetAttributeAsQuestId("QuestId", true, null) ?? 0;
			}
			catch
			{
				Logging.Write("Problem parsing a QuestId in behavior: Trouble Under Foot");
			}
		}
		public int QuestId { get; set; }
		private bool _isBehaviorDone;
		public int MobIdRogue = 6201;
		public int MobIdSatyr = 6200;
		public int MobIdHellcaller = 6202;
		public int MobIdRoach = 35628;
		public WoWPoint LocationHome = new WoWPoint(4527.815, -6340.137, 121.1725);
		private Composite _root;
		public QuestCompleteRequirement questCompleteRequirement = QuestCompleteRequirement.NotComplete;
		public QuestInLogRequirement questInLogRequirement = QuestInLogRequirement.InLog;
		public static void BPdiag(string message, params object[] args) { Logging.WriteDiagnostic(Colors.LawnGreen, "[QB DIAG TBM: " + message, args); }

		public override bool IsDone
		{
			get { return _isBehaviorDone; }
		}
		
		private LocalPlayer Me
		{
			get { return (StyxWoW.Me); }
		}

		public override void OnStart()
		{
			OnStart_HandleAttributeProblem();
			if (!IsDone)
			{
				if (TreeRoot.Current != null && TreeRoot.Current.Root != null && TreeRoot.Current.Root.LastStatus != RunStatus.Running)
				{
					var currentRoot = TreeRoot.Current.Root;
					if (currentRoot is GroupComposite)
					{
						var root = (GroupComposite)currentRoot;
						root.InsertChild(0, CreateBehavior());
					}
				}
				PlayerQuest Quest = StyxWoW.Me.QuestLog.GetQuestById((uint)QuestId);
				TreeRoot.GoalText = ((Quest != null) ? ("\"" + Quest.Name + "\"") : "In Progress");
			}
		}

		public WoWUnit Satyr
		{
			get
			{
				return ObjectManager.GetObjectsOfType<WoWUnit>().Where
					(u => (u.Entry == MobIdSatyr || u.Entry == MobIdRogue || u.Entry == MobIdHellcaller) && u.IsAlive && u.Distance < 200).FirstOrDefault();
			}
		}
		
		public WoWUnit Roach
		{
			get
			{
				return ObjectManager.GetObjectsOfType<WoWUnit>().Where
					(u => (u.Entry == MobIdRoach) && u.IsAlive && u.Distance < 200).FirstOrDefault();
			}
		}

		public bool IsQuestComplete()
		{
			var quest = StyxWoW.Me.QuestLog.GetQuestById((uint)QuestId);
			return quest == null || quest.IsCompleted;
		}

		public Composite DoneYet
		{
			get
			{
				return
					new Decorator(ret => IsQuestComplete(), new Action(delegate
					{
						TreeRoot.StatusText = "Finished!";
						_isBehaviorDone = true;
						return RunStatus.Success;
					}));
			}
		}

		public Composite CastAndKill
		{
			get
			{
				return new PrioritySelector(
					new Decorator(context => Roach == null && Satyr == null,
						new Action(context => Flightor.MoveTo(LocationHome))),
					new Decorator(context => Roach != null,
						new Action(context => 
						{
							if (Me.Location.Distance(Roach.Location) > 5)
								Flightor.MoveTo(Roach.Location);
							if (Me.Location.Distance(Roach.Location) <= 5)
							{
								WoWMovement.MoveStop();
								Roach.Interact();
							}
						})),
					new Decorator(context => Satyr != null,
						new Action(context => 
						{
							if (Me.Location.Distance(Satyr.Location) > 30)
								Flightor.MoveTo(Satyr.Location);
							if (Me.Location.Distance(Satyr.Location) <= 30)
							{
								WoWMovement.MoveStop();
								Satyr.Target();
								Lua.DoString("CastPetAction(1)");
								Thread.Sleep(1000);
							}
						})));
			}
		}

		protected override Composite CreateBehavior()
		{
			return _root ?? (_root = new Decorator(ret => !_isBehaviorDone,
				new PrioritySelector(
					new Decorator(context => !(Me.HasAura(69951)),
						new Action(context => _isBehaviorDone = true)),
					DoneYet,
					CastAndKill,
					new ActionAlwaysSucceed())));
		}
	}
}